<?php

declare(strict_types=1);

namespace App\Command;

use App\Model\Live;
use App\Model\Play;
use App\Model\User;
use App\Utils\Msg;
use Hyperf\Command\Command as HyperfCommand;
use Hyperf\Command\Annotation\Command;
use Psr\Container\ContainerInterface;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputOption;

#[Command]
class DevWsToolCommand extends HyperfCommand
{
    public function __construct(protected ContainerInterface $container)
    {
        parent::__construct('dev:ws-tool');
    }

    public function configure()
    {
        parent::configure();
        $this->addOption('entry', 'e', InputOption::VALUE_NONE, '进入房间');
        $this->addOption('send', 's', InputOption::VALUE_NONE, '发送消息');
        $this->addOption('heartbeat', 'h', InputOption::VALUE_NONE, '心跳');
        $this->addArgument('uid', InputArgument::OPTIONAL, 'UID', 0);
        $this->addArgument('match_id', InputArgument::OPTIONAL, '比赛ID或房间号', '');

        $this->addArgument('msg', InputArgument::OPTIONAL, '消息内容', '');
        $this->setDescription('websocket测试工具');
    }

    public function handle()
    {
        if ($this->option('entry')) {
            $uid = (int)$this->input->getArgument('uid');
            if (!$uid) {
                $this->output->error('请传入uid参数');
                return;
            }
            $matchId = $this->input->getArgument('match_id');
            if (empty($matchId)) {
                $this->output->error('请传入比赛ID或房间号');
                return;
            }

            $userInfo = User::find($uid);
            if (empty($userInfo)) {
                $this->output->error('用户不存在');
                return;
            }

            if (preg_match('/\d+/i', $matchId) && !Live::query()->where('room_id', $matchId)->exists()) {
                $this->output->error('房间不存在');
                return;
            } elseif (!Play::query()->where('match_id', $matchId)->exists()) {
                $this->output->error('比赛不存在');
                return;
            }
            $msg = Msg::Create('UserEntryCmd', [
                'appUserId' => '',
                'deviceId' => ''
            ]);


        }
    }
}
